package com.qianfeng.test.task;

import android.os.AsyncTask;
import android.text.TextUtils;
import android.widget.BaseAdapter;

import com.alibaba.fastjson.JSONObject;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.qianfeng.test.bean.AppBean;
import com.qianfeng.test.bean.ShopInfo;
import com.qianfeng.test.utils.HttpUtils;




import java.util.List;

/**
 * Created by Administrator on 15-7-20.
 */
public class MainTask extends AsyncTask<String ,Void,List<ShopInfo>>{
    private BaseAdapter baseAdapter;
    private List<ShopInfo>shopInfoList;
    private PullToRefreshListView ptrListView;
    private int page;

    public MainTask(BaseAdapter baseAdapter,List<ShopInfo>shopInfoList,PullToRefreshListView ptrListView,int page) {
        this.baseAdapter = baseAdapter;
        this.shopInfoList = shopInfoList;
        this.ptrListView = ptrListView;
        this.page = page;
    }

    @Override
    protected List<ShopInfo> doInBackground(String... params) {
        List<ShopInfo>shopInfoList = null;
        String result = HttpUtils.getDataFromService(params[0]);
        if(!TextUtils.isEmpty(result)){
            AppBean app = JSONObject.parseObject(result, AppBean.class);
            if(app!=null&&app.getResult()!=null){//
                shopInfoList = app.getResult().getData();//
            }
        }
        System.out.print("shopInfoList...."+shopInfoList);
        return shopInfoList;
    }

    @Override
    protected void onPostExecute(List<ShopInfo> shopInfos) {
        super.onPostExecute(shopInfos);
        ptrListView.onRefreshComplete();//一定要写
        if(shopInfos!=null&&shopInfos.size()>0){
            System.out.print("onPostExecute....进来了");
            if(page == 1 && shopInfoList.size()>0){//什么意思
                shopInfoList.clear();
            }
            shopInfoList.addAll(shopInfos);
            baseAdapter.notifyDataSetChanged();
        }

    }
}
